package com.sdruixinggroup.mondayb2b.utils;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Environment;
import android.text.TextUtils;

import com.sdruixinggroup.mondayb2b.ui.Activities.LoginActivity;

/**
 * Created by 00027450 on 2017/2/10.
 *
 */

public class UserInfoUtil {


    public static final String data = "nongzhi.shareperenect";
    public static final String phone = "phone";
    public static final String token = "token";
    public static final String reflush_token = "reflush_token";
    public static final String PIC_SAVE_PATH = Environment.getExternalStorageDirectory().getAbsolutePath()+"/nzpc/";
    public static int AREA_CODE = 1;
    public static String  AREA_CODE_NAME = "苏州吴中区";
    public static double mLatitude = 0;
    public static double mLongitude = 0;
    /*
   * 手机号码的正则表达式
   * */
//    public static final String PHONE_REGEX = "13\\d{9}|14[57]\\d{8}|15[012356789]\\d{8}|18[0123456789]\\d{8}|17[03678]\\d{8}";
    public static final String PHONE_REGEX = "13\\d{9}|14\\d{9}|15\\d{9}|18\\d{9}|17\\d{9}|19\\d{9}|16\\d{9}";





    public static void saveToken(Context context,String mytoken){
        SharedPreferences pref = context.getSharedPreferences(data,Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = pref.edit();
        editor.putString(token,mytoken);
        editor.commit();
    }


    public static void saveReflushToken(Context context,String mytoken){
        SharedPreferences pref = context.getSharedPreferences(data,Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = pref.edit();
        editor.putString(reflush_token,mytoken);
        editor.commit();
    }



    /*
   * 获取用户token
   * */
    public static String getInfoToken(Context mContext){
        SharedPreferences pref = mContext.getSharedPreferences(data,Context.MODE_PRIVATE);
        return pref.getString(token,"");
    }



    /*
    * 获取用户电话
    * */
    public static String getInfoPhone(Context mContext){
        SharedPreferences pref = mContext.getSharedPreferences(data,Context.MODE_PRIVATE);
        return pref.getString(phone,"");
    }


    /*
 * 获取是否已经登录过得boolean
 * */
    public static boolean getIsLogin(Context mContext){

        String token = getInfoToken(mContext);
        if(TextUtils.isEmpty(token)){
            return false;
        }else {
            return true;
        }

    }
//err_code=10003
    public static final int TOKEN_OUT_TIME = 10003;
    public static final int TOKEN_OUT_TIME_ERROR = 10002;
    public static void intoLogin(Activity context){
        SharedPreferences pref = context.getSharedPreferences(data,Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = pref.edit();
        editor.clear();
        editor.commit();
        Intent intent = new Intent(context, LoginActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        context.startActivity(intent);
    }

}
